package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.MyInterestVideoBean;
import com.beiing.tianshuai.tianshuai.widget.RoundAngleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Jinni on 2017/12/8 0008
 */

public class MyInterestContentAdapter extends BaseRecyclerViewAdapter<MyInterestVideoBean.DataBean.VideoBean> {

    private Context mContext;

    private OnDeleteClickListener mListener;
    private OnItemClickListener mOnItemClickListener;

    public MyInterestContentAdapter(Context context) {
        mContext = context;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void setListener(OnDeleteClickListener listener) {
        mListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        int result = 0;
        MyInterestVideoBean.DataBean.VideoBean bean = mList.get(position);
        String status = bean.getStatus();
        switch (status) {
            case "0":
                result = 0;
                break;
            case "1":
                result = 1;
                break;
            case "2":
                result = 2;
                break;
        }
        return result;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder result = null;
        switch (viewType) {
            case 0:
                result = new VideoViewHolder(getConvertView(mContext, R.layout.item_my_video_under_review, null));
                break;
            case 1:
                result = new VideoViewHolder(getConvertView(mContext, R.layout.item_my_video_review_success, null));
                break;
            case 2:
                result = new VideoViewHolder(getConvertView(mContext, R.layout.item_my_video_review_failed, null));
                break;
        }
        return result;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VideoViewHolder) {
            MyInterestVideoBean.DataBean.VideoBean bean = mList.get(position);
            ((VideoViewHolder) holder).setData(bean);
            ((VideoViewHolder) holder).setListener(bean);
        }
    }

    class VideoViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.btn_delete)
        TextView mBtnDelete;
        @BindView(R.id.riv_cover)
        RoundAngleImageView mRivCover;
        @BindView(R.id.tv_title)
        TextView mTvTitle;

        VideoViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(MyInterestVideoBean.DataBean.VideoBean bean) {
            mTvTitle.setText(bean.getTitle());
            RequestOptions avatarOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .override(500, 280);
            String photo = bean.getCover();
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(photo) ? R.mipmap.default_avatar : HttpRequestConstant.VIDEO_PIC_HEAD + photo)
                    .apply(avatarOptions)
                    .into(mRivCover);
        }

        public void setListener(final MyInterestVideoBean.DataBean.VideoBean bean) {
            mBtnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onDeleteClick(bean.getId(), getAdapterPosition());
                    }
                }
            });

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null && bean.getStatus().equals("1")) {
                        mOnItemClickListener.onItemClick(bean.getId(), getAdapterPosition(), bean.getTname());
                    }
                }
            });
        }
    }

    public interface OnDeleteClickListener {
        void onDeleteClick(String id, int position);
    }

    public interface OnItemClickListener {
        void onItemClick(String id, int position, String typeName);
    }
}
